package com.caibq.controller;

import com.caibq.entity.User;
import javassist.compiler.ast.Variable;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * projectName: demoBox
 *
 * @author: caicai
 * time: 2024/6/21 18:10 周五
 * description:
 */
public class ReflectionExample {
    public static void main(String[] args) throws Exception {
        Class<?> myClass = Class.forName("com.caibq.entity.User");

        Constructor<?> constructor = myClass.getConstructor();
        Object myObject = constructor.newInstance();

        Method myMethod = myClass.getMethod("getName", String.class);
        myMethod.invoke(myObject, "caicai");

        Field myField = myClass.getDeclaredField("name");
        myField.setAccessible(true);
        myField.set(myObject, "new caicai");


    }
}
